﻿Public Class OPERATEITEM
    Dim connect As CONDB = CONDB.NewConnection()
    Dim s As FILTERCLASS
    Dim Sql As String
    Dim PROVIDER_Check As New PROVIDERSCLASS
    Dim datejaja As New FriendsDate()
    'ไว้สำหรับมาร์คว่ากำลังแก้ไขอยู่ หากทำการ cellclick event ใน checkbox จะได้ไม่ถูก trigger
    Dim cellClickEvent As Integer = 0

    Private Sub operateGroup()
        Dim sql2 As String
        sql2 = "SELECT `grpid`, `code`, `grpname` FROM masgrpoper WHERE STATUS = '1'"
        s = New FILTERCLASS(GRPNAMETxt1, sql2, "รหัสกลุ่มหัตถการ,โค้ดกลุ่มหัตถการ,ชื่อกลุ่มหัตถการ", "50,100,130", "1,1,1", "1,1,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
    End Sub

    Private Sub OPERATEITEM_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridoperat()

        SearchCCODETxt2()
        SearchRCODETxt2()
        operateGroup()

        DesignForm(Me)
        DesignGrid(OPERATITEMGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub
    Private Sub SelectDatagridoperat()
        Dim objSelectDataOperatitem As New OPERATITEM
        Dim OperatData As DataTable
        datejaja.culTure_TH()
        datejaja.TO_TH()
        OperatData = connect.GetTable(objSelectDataOperatitem.SelectOperatitem_)
        OPERATITEMGrid.PrimaryGrid.DataSource = OperatData
    End Sub

    Private Sub addOperatitem_Click(sender As Object, e As EventArgs) Handles addOperatitem.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PADD_ = True Then
                If CODETxt.Text.ToString.Length > 0 And OPERATENAME_ENTxt.Text.ToString.Length > 0 Then
                    Dim objInsertData As New OPERATITEM
                    objInsertData.CODE_ = CODETxt.Text
                    objInsertData.ICD10TM_ = ICD10Txt.Text
                    objInsertData.ICD9CM_ = ICD9Txt.Text
                    objInsertData.FCODE_ = FCODETxt.Text
                    objInsertData.OPERATENAME_EN_ = OPERATENAME_ENTxt.Text
                    objInsertData.OPERATENAME_TH_ = OPERATENAME_THTxt.Text
                    objInsertData.REF_ = REFTxt.Text
                    objInsertData.STTDATE_ = DateAdd(DateInterval.Year, -543, STTDATE.Value).ToString("yyyy/MM/dd")
                    objInsertData.EXPDATE_ = DateAdd(DateInterval.Year, -543, EXPDATE.Value).ToString("yyyy/MM/dd")

                    If COSTTxt.Text.Trim = "" Then
                        objInsertData.COST_ = 0
                    Else
                        objInsertData.COST_ = COSTTxt.Text
                    End If

                    objInsertData.F_FC_ = ckbF_FC.Checked
                    objInsertData.STATUS_ = OperatStatusChk.Checked
                    objInsertData.CHKIN_ = OperatCheckinChk.Checked
                    If Convert.ToString(CCODETxt2.Tag).Trim <> "" Then
                        objInsertData.CCODE_ = Convert.ToString(CCODETxt2.Tag).Trim
                    End If
                    If Convert.ToString(RCODETxt2.Tag).Trim <> "" Then
                        objInsertData.RCODE_ = Convert.ToString(RCODETxt2.Tag).Trim
                    End If

                    If OPDPRCTxt.Text.Trim = "" Then
                        objInsertData.OPDPRC_ = 0
                    Else
                        objInsertData.OPDPRC_ = OPDPRCTxt.Text.Trim
                    End If

                    If IPDPRCTxt.Text.Trim = "" Then
                        objInsertData.IPDPRC_ = 0
                    Else
                        objInsertData.IPDPRC_ = IPDPRCTxt.Text.Trim
                    End If

                    objInsertData.F_STK_ = F_STK.Checked
                    objInsertData.F_DF_ = ckbDoctorFee.Checked
                    objInsertData.GRPID_ = Convert.ToString(GRPNAMETxt1.Tag).Trim

                    If objInsertData.insertOperatitemData() = True Then
                        SelectDatagridoperat()
                        cleardataOperat()
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
            Else
                MsgBox(PROVIDER_Check.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editOperatitem_Click(sender As Object, e As EventArgs) Handles editOperatitem.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If OPERATIDTxt.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                    If editStat = 1 Then
                        Dim objeditData As New OPERATITEM
                        objeditData.OPERATID_ = OPERATIDTxt.Text
                        objeditData.CODE_ = CODETxt.Text
                        objeditData.ICD10TM_ = ICD10Txt.Text
                        objeditData.ICD9CM_ = ICD9Txt.Text
                        objeditData.FCODE_ = FCODETxt.Text
                        objeditData.OPERATENAME_EN_ = OPERATENAME_ENTxt.Text
                        objeditData.OPERATENAME_TH_ = OPERATENAME_THTxt.Text
                        objeditData.REF_ = REFTxt.Text
                        objeditData.STTDATE_ = DateAdd(DateInterval.Year, -543, STTDATE.Value).ToString("yyyy/MM/dd")
                        objeditData.EXPDATE_ = DateAdd(DateInterval.Year, -543, EXPDATE.Value).ToString("yyyy/MM/dd")

                        If COSTTxt.Text.Trim = "" Then
                            objeditData.COST_ = 0
                        Else
                            objeditData.COST_ = COSTTxt.Text
                        End If

                        objeditData.F_FC_ = ckbF_FC.Checked
                        objeditData.CHKIN_ = OperatCheckinChk.Checked
                        objeditData.STATUS_ = OperatCheckinChk.Checked
                        objeditData.OPERATENAME_EN_ = OPERATENAME_ENTxt.Text
                        If Convert.ToString(CCODETxt2.Tag) <> "" Then
                            objeditData.CCODE_ = Convert.ToString(CCODETxt2.Tag)
                        End If
                        If Convert.ToString(RCODETxt2.Tag) <> "" Then
                            objeditData.RCODE_ = Convert.ToString(RCODETxt2.Tag)
                        End If
                        objeditData.PRDCODE_ = PRDCODETxt.Text

                        If OPDPRCTxt.Text.Trim = "" Then
                            objeditData.OPDPRC_ = 0
                        Else
                            objeditData.OPDPRC_ = OPDPRCTxt.Text.Trim
                        End If

                        If IPDPRCTxt.Text.Trim = "" Then
                            objeditData.IPDPRC_ = 0
                        Else
                            objeditData.IPDPRC_ = IPDPRCTxt.Text.Trim
                        End If

                        objeditData.F_STK_ = F_STK.Checked
                        objeditData.F_DF_ = ckbDoctorFee.Checked
                        objeditData.GRPID_ = Convert.ToString(GRPNAMETxt1.Tag).Trim
                        If objeditData.editOperatitemData() = True Then
                            SelectDatagridoperat()
                            cleardataOperat()
                        End If
                    End If
                Else
                    MsgBox("โปรดระบุข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical)
                End If
                addOperatitem.Enabled = True
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub
    Private Sub cleardataOperat()
        CODETxt.Text = ""
        OPERATIDTxt.Text = ""
        ICD10Txt.Text = ""
        ICD9Txt.Text = ""
        FCODETxt.Text = ""
        OPERATENAME_ENTxt.Text = ""
        OPERATENAME_THTxt.Text = ""
        REFTxt.Text = ""
        Dim sqldate As String
        Dim dt As New DataTable
        sqldate = " SELECT CURDATE() "
        dt = connect.GetTable(sqldate)
        STTDATE.Value = Convert.ToDateTime(dt.Rows(0)(0))
        EXPDATE.Value = Convert.ToDateTime(dt.Rows(0)(0))
        PRDCODETxt.Text = ""
        COSTTxt.Text = ""
        OperatCheckinChk.Checked = False
        OperatStatusChk.Checked = False
        F_STK.Checked = False
        CCODETxt2.Text = ""
        RCODETxt2.Text = ""
        OPDPRCTxt.Text = ""
        IPDPRCTxt.Text = ""
        PRDCODETxt.Text = ""
        GRPNAMETxt1.Text = ""
        ckbF_FC.Checked = False
    End Sub
    Private Sub SearchCCODETxt2()
        Dim sql2 As String
        sql2 = "SELECT cast(`ccode` as nchar(3)) as ccode,`name`FROM finchcode WHERE `STATUS` = 1 "
        s = New FILTERCLASS(CCODETxt2, sql2, "รหัส,รหัสรายได้", "50,130", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
        s.SetShowInDown = False
    End Sub
    Private Sub SearchRCODETxt2()
        Dim sql2 As String
        sql2 = "SELECT cast(`rcode` as nchar(3)) as rcode,`name`FROM finrecode WHERE `STATUS` = 1 "
        s = New FILTERCLASS(RCODETxt2, sql2, "รหัส,รหัสรายรับทางการเงิน", "50,163", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
        s.SetShowInDown = False
    End Sub
    Private Sub btnClsxray_Click(sender As Object, e As EventArgs)
        Dim nextform As New IMPORTEXCEL(operatitem.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub OPERATITEMGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles OPERATITEMGrid.CellClick
        cellClickEvent = 1

        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหัตถการ").Value() IsNot DBNull.Value Then
            OPERATIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสหัตถการ").Value()
        Else
            OPERATIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODE").Value() IsNot DBNull.Value Then
            CODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODE").Value()
        Else
            CODETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("PRDCODE").Value() IsNot DBNull.Value Then
            PRDCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("PRDCODE").Value()
        Else
            PRDCODETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ICD10TM").Value() IsNot DBNull.Value Then
            ICD10Txt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ICD10TM").Value()
        Else
            ICD10Txt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ICD9CM").Value() IsNot DBNull.Value Then
            ICD9Txt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ICD9CM").Value()
        Else
            ICD9Txt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("FCODE").Value() IsNot DBNull.Value Then
            FCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("FCODE").Value()
        Else
            FCODETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหัตถการภาษาอังกฤษ").Value() IsNot DBNull.Value Then
            OPERATENAME_ENTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหัตถการภาษาอังกฤษ").Value()
        Else
            OPERATENAME_ENTxt.Text = ""
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหัตถการภาษาไทย").Value() IsNot DBNull.Value Then
            OPERATENAME_THTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อหัตถการภาษาไทย").Value()
        Else
            OPERATENAME_THTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value() IsNot DBNull.Value Then
            REFTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("หมายเหตุ").Value()
        Else
            REFTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันที่เริ่มใช้").Value() IsNot DBNull.Value Then
            STTDATE.Value = Convert.ToDateTime(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันที่เริ่มใช้").Value())
        Else
            STTDATE.Value = DateAndTime.Now
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันที่เลิกใช้").Value() IsNot DBNull.Value Then
            EXPDATE.Value = Convert.ToDateTime(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("วันที่เลิกใช้").Value())
        Else
            EXPDATE.Value = DateAndTime.Now
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ต้นทุน").Value() IsNot DBNull.Value Then
            COSTTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ต้นทุน").Value()
        Else
            COSTTxt.Text = ""
        End If
        If grid.Cells("CCODE").Value() IsNot DBNull.Value Then
            CCODETxt2.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CCODE").Value()
        Else
            CCODETxt2.Tag = ""
        End If
        If grid.Cells("รหัสรายได้").Value() IsNot DBNull.Value Then
            CCODETxt2.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value()
        Else
            CCODETxt2.Text = ""
        End If
        If grid.Cells("CCODE").Value() IsNot DBNull.Value Then
            RCODETxt2.Tag = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("RCODE").Value()
        Else
            RCODETxt2.Tag = ""
        End If
        If grid.Cells("รหัสรายรับทางการเงิน").Value() IsNot DBNull.Value Then
            RCODETxt2.Text = CType(e.GridPanel.Rows(e.GridCell.RowIndex), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Value()
        Else
            RCODETxt2.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคาขาย OPD").Value() IsNot DBNull.Value Then
            OPDPRCTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคาขาย OPD").Value()
        Else
            OPDPRCTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคาขาย IPD").Value() IsNot DBNull.Value Then
            IPDPRCTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ราคาขาย IPD").Value()
        Else
            IPDPRCTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("grpid").Value() IsNot DBNull.Value Then
            GRPNAMETxt1.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("grpid").Value()
        Else
            GRPNAMETxt1.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("code กลุ่มหัตถการ").Value() IsNot DBNull.Value Then
            GRPNAMETxt1.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("code กลุ่มหัตถการ").Value()
        Else
            GRPNAMETxt1.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            OperatStatusChk.Checked = True
        Else
            OperatStatusChk.Checked = False
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตัดสต๊อค").Value() = True Then
            F_STK.Checked = True
        Else
            F_STK.Checked = False
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CHKIN").Value() = True Then
            OperatCheckinChk.Checked = True
        Else
            OperatCheckinChk.Checked = False
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("Doctor Fee").Value() = True Then
            ckbDoctorFee.Checked = True
        Else
            ckbDoctorFee.Checked = False
        End If

        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เพิ่มในการเงิน").Value() = True Then
            ckbF_FC.Checked = True
        Else
            ckbF_FC.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE OPERATITEM SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `OPERATID` = '" & OPERATIDTxt.Text & "' ;"

                Sql += "UPDATE MASPRODUCT SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`STATUS` = 1 "
                Else
                    Sql += "`STATUS` = 0 "
                End If
                Sql += "WHERE `PRDCODE` = '" & PRDCODETxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        addOperatitem.Enabled = False

        cellClickEvent = 0
    End Sub

    Private Sub CCODETxt2_TextChanged(sender As Object, e As EventArgs) Handles CCODETxt2.TextChanged
        If Convert.ToString(CCODETxt2.Tag).Trim = "" Then
            RCODETxt2.Enabled = False
            RCODETxt2.Text = ""
            Return
        End If
        RCODETxt2.Enabled = True
        Dim sql2 As String
        sql2 = "SELECT cast(`rcode` as nchar(3)) as rcode,`name`FROM finrecode WHERE `ccode` = '" & Convert.ToString(CCODETxt2.Tag).Trim & "'  and  `STATUS` = 1 "
        s = New FILTERCLASS(RCODETxt2, sql2, "รหัส,รหัสรายรับทางการเงิน", "50,130", "1,1", "0,1")
        s.SetTagIndex = 0 ' settag
        s.SetTextIndex = 1 'settext
        s.SetShowBorder = True
        s.SetShowInDown = False
    End Sub

    Private Sub GRPNAMETxt1_TextChanged(sender As Object, e As EventArgs) Handles GRPNAMETxt1.TextChanged
        If cellClickEvent = 0 Then
            Dim sql As String = "SELECT COUNT(operatid) + 1 AS 'groupNum' FROM operatitem a WHERE `code` LIKE '" & GRPNAMETxt1.Text & "%'"
            Dim dt As DataTable = connect.GetTable(sql)
            Dim newNum As String

            If dt.Rows.Count > 0 Then
                newNum = dt.Rows(0)(0)

                If newNum.Length.ToString = 1 Then
                    newNum = "000" & newNum
                ElseIf newNum.Length.ToString = 2 Then
                    newNum = "00" & newNum
                ElseIf newNum.Length.ToString = 3 Then
                    newNum = "0" & newNum
                End If

                CODETxt.Text = GRPNAMETxt1.Text & newNum
                OPERATENAME_ENTxt.Select()
            Else
                CODETxt.Select()
                connect.Dispose()
                Exit Sub
            End If

            connect.Dispose()
        End If
    End Sub

    'Private Sub OPDPRCTxt_KeyPress(sender As Object, e As KeyPressEventArgs) Handles OPDPRCTxt.KeyPress
    '    If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
    '        e.Handled = True
    '    End If
    'End Sub

    'Private Sub IPDPRCTxt_KeyPress(sender As Object, e As KeyPressEventArgs) Handles IPDPRCTxt.KeyPress
    '    If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
    '        e.Handled = True
    '    End If
    'End Sub
End Class